#include "keyscan.h"

#include "bsp_led.h"
#include "bsp_key.h"

u8 isKeyUp = 0;
u8 isKeyDown = 0;
u8 isKeyLeft = 0;
u8 isKeyRight = 0;
u8 isKeyOk = 0;

void KeyUp()
{
	if(isKeyUp == 0)
		isKeyUp=1;
	LED=!LED;
}

void KeyDown()
{
	if(isKeyDown == 0)
		isKeyDown=1;
	LED = !LED;
}

void KeyLeft()
{
	if(isKeyLeft == 0)
		isKeyLeft=1;
	LED = !LED;
}

void KeyRight()
{
	if(isKeyRight == 0)
		isKeyRight=1;
	LED = !LED;
}

void KeyOk()
{
	if(isKeyOk == 0)
		isKeyOk=1;
	LED = !LED;
}

void KeyScan()
{
    static int keyCount = 0;
    static int keyState = 0;
    if (KEY_UP == 0 && keyState == 0) //按键按下
    {
        keyCount++;
        if (keyCount > 2 && KEY_UP == 0 && keyState == 0) //加两次类似延迟10ms，不好解释
        {
            /*ToDo:按键按下执行的操作*/
            KeyUp();
            keyState = 1;
        }
    }
    else if (KEY_DOWN == 0 && keyState == 0)
    {
        keyCount++;
        if (keyCount > 2 && KEY_DOWN == 0 && keyState == 0)
        {
            /*ToDo:按键按下执行的操作*/
            KeyDown();
            keyState = 1;
        }
    }
    else if (KEY_LEFT == 0 && keyState == 0)
    {
        keyCount++;
        if (keyCount > 2 && KEY_LEFT == 0 && keyState == 0)
        {
            /*ToDo:按键按下执行的操作*/
            
			KeyLeft();
            keyState = 1;
        }
    }
    else if (KEY_RIGHT == 0 && keyState == 0)
    {
        keyCount++;
        if (keyCount > 2 && KEY_RIGHT == 0 && keyState == 0)
        {
            /*ToDo:按键按下执行的操作*/
            
			KeyRight();
            keyState = 1;
        }
    }else if (KEY_OK == 0 && keyState == 0)
    {
        keyCount++;
        if (keyCount > 2 && KEY_OK == 0 && keyState == 0)
        {
            /*ToDo:按键按下执行的操作*/
            
			KeyOk();
            keyState = 1;
        }
    }
	else if (KEY_UP == 1 && KEY_DOWN == 1 && KEY_LEFT == 1 && KEY_RIGHT == 1 && KEY_OK == 1 && keyState == 1) //当所有按键都处于抬起状态，状态刷新
    {
        keyCount = 0;
        keyState = 0;
    }
}